<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="hAdd"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="list">
              <el-table-column label="序号" type="index" width="50" />
              <el-table-column prop="name" label="角色名称" width="180" />
              <el-table-column prop="description" label="描述" />
              <el-table-column width="300" label="操作">
                <template #default="{row}">
                  <el-button size="mini" type="success" @click="hAssign(row.id)">分配权限</el-button>
                  <el-button size="mini" @click="hEdit(row.id)">编辑</el-button>
                  <el-button size="mini" type="danger" @click="delRole(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                :current-page="pageParams.page"
                :page-sizes="[2, 3, 5, 10]"
                :page-size="pageParams.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <el-dialog
        :title="isEdit?'编辑':'新增'"
        :visible.sync="showDialog"
        @close="hClose"
      >
        <el-form
          ref="roleForm"
          :model="roleForm"
          :rules="rules"
          label-width="100px"
        >
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="showDialog=false">取消</el-button>
            <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
      <el-dialog v-if="showAssignDialog" title="分配权限" :visible.sync="showAssignDialog">
        <AssignPermission :role-id="roleId" @close="showAssignDialog=false" />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getRole, addRole, delRoleById, getRoleByIdDetail, editRoleById } from '@/api/role'
import AssignPermission from './assignPermission'
export default {
  components: {
    AssignPermission
  },
  data() {
    return {
      // 表单验证
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '新增角色必填', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '角色描述必填', trigger: 'blur' }
        ]
      },
      // 控制弹层
      showDialog: false,
      showAssignDialog: false,
      // 获取角色列表参数
      pageParams: {
        page: 1,
        pagesize: 2
      },
      // 储存列表参数
      list: [],
      // 储存角色总数
      total: 0,
      // 控制是否编辑
      isEdit: false,
      roleId: ''

    }
  },
  created() {
    this.loadRole()
  },
  methods: {
    hAssign(id) {
      this.roleId = id
      this.showAssignDialog = true
    },
    hClose() {
      this.$refs.roleForm.resetFields()
    },
    hAdd() {
      this.showDialog = true
      this.isEdit = false
    },
    hEdit(id) {
      this.showDialog = true
      this.isEdit = true
      this.loadEditRoleById(id)
    },
    // 删除角色
    async delRole(id) {
      try {
        await this.$confirm('你确定删除吗?')
        const res = await delRoleById(id)
        console.log('删除角色', res)
        if (this.list.length === 1) {
          this.pageParams.page--
          if (this.pageParams.page <= 0) {
            this.pageParams.page = 1
          }
        }
        this.loadRole()
      } catch (error) {
        console.log(error)
      }
    },
    // 兜底验证
    hSubmit() {
      this.$refs.roleForm.validate((valid) => {
        if (!valid) return
        this.isEdit ? this.loadEditRole() : this.loadAddRole()
      })
    },
    // 根据id获取角色详情
    async loadEditRoleById(id) {
      try {
        const res = await getRoleByIdDetail(id)
        console.log('根据id获取角色详情', res)
        this.roleForm = res.data
      } catch (error) {
        console.log(error)
      }
    },
    // 编辑角色
    async loadEditRole() {
      try {
        const res = await editRoleById(this.roleForm)
        console.log('新增角色', res)
        this.showDialog = false
        this.loadRole()
      } catch (error) {
        console.log(error)
      }
    },
    // 新增角色
    async loadAddRole() {
      try {
        const res = await addRole(this.roleForm)
        console.log('新增角色', res)
        this.total++
        this.pageParams.page = Math.ceil(this.total / this.pageParams.pagesize)
        this.loadRole()
        this.showDialog = false
      } catch (error) {
        console.log(error)
      }
    },
    // 分页功能
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.pageParams.page = 1
      this.pageParams.pagesize = val
      this.loadRole()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.pageParams.page = val
      this.loadRole()
    },
    // 获取角色列表
    async loadRole() {
      try {
        const res = await getRole(this.pageParams)
        console.table('角色列表', res)
        this.list = res.data.rows
        this.total = res.data.total
      } catch (error) {
        console.log(error)
      }
    }

  }
}
</script>
